//
//  XLPPGuardianListView.m
//  XiangLove
//
//  Created by 陈宪栋 on 2017/12/19.
//  Copyright © 2017年 BAOMIHUA.COM. All rights reserved.
//

#import "XLPPGuardianListView.h"
#import "XLPPGuardianModel.h"

@interface XLPPGuardianCollectionCell : UICollectionViewCell
@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UILabel *suffixLabel;
@end

@implementation XLPPGuardianCollectionCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _avatarImageView = [[UIImageView alloc] init];
        _avatarImageView.layer.cornerRadius = 20;
        _avatarImageView.layer.masksToBounds = YES;
        [self addSubview:_avatarImageView];
        [_avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        _suffixLabel = [[UILabel alloc] init];
        _suffixLabel.textAlignment = NSTextAlignmentCenter;
        _suffixLabel.backgroundColor = [UIColor greenColor];
        _suffixLabel.font = [UIFont systemFontOfSize:10];
        _suffixLabel.layer.cornerRadius = 7;
        _suffixLabel.layer.masksToBounds = YES;
        [self addSubview:_suffixLabel];
        [_suffixLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(14, 14));
            make.center.equalTo(_avatarImageView).centerOffset(CGPointMake(12, 12));
        }];
    }
    return self;
}
@end


@interface XLPPGuardianListView ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *arrowImageView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
@property (nonatomic, strong) NSArray *colorArray;
@end

@implementation XLPPGuardianListView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        self.colorArray = @[
                            @{
                                @"fillColor"   : RGB(255, 192, 40),
                                @"borderColor" : RGB(255, 233, 47)
                                },
                            @{
                                @"fillColor"   : RGB(103, 191, 192),
                                @"borderColor" : RGB(143, 238, 139)
                                },
                            @{
                                @"fillColor"   : RGB(189, 138, 112),
                                @"borderColor" : RGB(239, 181, 136)
                                },
                            @{
                                @"fillColor"   : RGB(160, 199, 112),
                                @"borderColor" : RGB(204, 237, 146)
                                },
                            @{
                                @"fillColor"   : RGB(137, 179, 231),
                                @"borderColor" : RGB(175, 211, 255)
                                }
                            ];
    }
    return self;
}

- (void)setupView {
    self.backgroundColor = XLThemeColor;
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont systemFontOfSize:14];
    _titleLabel.textColor = RGB(144, 144, 144);
    _titleLabel.text = @"守护榜";
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.mas_top).offset(13);
    }];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(40, 40);
    layout.minimumInteritemSpacing = 12;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.userInteractionEnabled = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    [_collectionView registerClass:[XLPPGuardianCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([XLPPGuardianCollectionCell class])];
    [self addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(45, 15, 25, 30));
    }];
    
    _arrowImageView = [[UIImageView alloc] init];
    _arrowImageView.image = [UIImage imageNamed:@"xl_arrow_icon"];
    [self addSubview:_arrowImageView];
    [_arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(17, 16));
        make.centerY.equalTo(_collectionView.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-10);
    }];
    
    _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    _indicatorView.hidden = YES;
    [self addSubview:_indicatorView];
    [_indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:self];
//    if (CGRectContainsPoint(self.collectionView.frame, point) == NO) {
        if ([self.delegate respondsToSelector:@selector(guardianListViewSelected)]) {
            [self.delegate guardianListViewSelected];
        }
//    }
}

- (void)showLoading {
    _indicatorView.hidden = NO;
    if ([_indicatorView isAnimating] == NO) {
        [_indicatorView startAnimating];
    }
}

- (void)hideLoading {
    _indicatorView.hidden = YES;
    if ([_indicatorView isAnimating]) {
        [_indicatorView stopAnimating];
    }
}

- (void)reloadData {
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.nightArray.count > 5 ? 5 : self.nightArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XLPPGuardianCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XLPPGuardianCollectionCell class]) forIndexPath:indexPath];
//    XLPPGuardianModel *model = self.dataArray[indexPath.row];
//    KnightModel *model = self.nightArray[indexPath.row];
    
    NSDictionary *dic = self.nightArray[indexPath.row];
    
    [cell.avatarImageView setImageWithURL:[NSURL URLWithString:dic[@"Url"]] placeholder:[UIImage imageNamed:@""]];
    
    NSString *tempString = [NSString stringWithFormat:@"%@",dic[@"Url"]];
    
    tempString = [tempString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *stringurl = [NSURL URLWithString:tempString];
    
    [cell.avatarImageView sd_setImageWithURL:stringurl];
    
    
//    [cell.avatarImageView setImageWithURL:stringurl placeholder:[UIImage imageNamed:@""]];
    
    cell.suffixLabel.text = F(@"%zd", indexPath.row + 1);
    UIColor *borderColor = [self.colorArray[indexPath.row] objectForKey:@"borderColor"];
    UIColor *fillColor = [self.colorArray[indexPath.row] objectForKey:@"fillColor"];
    cell.avatarImageView.layer.borderColor = borderColor.CGColor;
    cell.avatarImageView.layer.borderWidth = 1;
    cell.suffixLabel.backgroundColor = fillColor;
    cell.suffixLabel.layer.borderWidth = 1;
    cell.suffixLabel.layer.borderColor = fillColor.CGColor;
    return cell;
}

#pragma mark - UICollectionViewDelegate
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    if ([self.delegate respondsToSelector:@selector(guardianListView:didSelectItemAtIndexPath:)]) {
//        [self.delegate guardianListView:self didSelectItemAtIndexPath:indexPath];
//    }
//}
@end
